﻿using System;
using fastCSharp;
using fastCSharp.code.cSharp;

namespace diantou.web.onlineJudge
{
    /// <summary>
    /// 用户OJ问题
    /// </summary>
    [fastCSharp.code.cSharp.webView.clientType(Name = "diantou.userProblem")]
    struct userProblem
    {
        /// <summary>
        /// 相关收藏夹
        /// </summary>
        public struct relateFavorite
        {
            /// <summary>
            /// 是否重新加载视图
            /// </summary>
            [fastCSharp.code.cSharp.webView.outputAjax]
            public bool ViewOnly;
            /// <summary>
            /// 用户OJ问题
            /// </summary>
            public userProblem Problem;
            /// <summary>
            /// 相关收藏top5
            /// </summary>
            private diantou.dataAccess.favorite[] top5;
            /// <summary>
            /// 相关收藏top5
            /// </summary>
            public diantou.dataAccess.favorite[] Top5
            {
                get
                {
                    if (ViewOnly) return null;
                    if (top5 == null)
                    {
                        int count;
                        top5 = diantou.dataAccess.clientCache.Favorite.Get(diantou.dataAccess.tcpCall.favorite.relateIds(diantou.dataModel.favorite.type.Problem, Problem.Problem.Id, 5, 1, Problem.UserId, out count));
                        if (top5 == null) top5 = nullValue<diantou.dataAccess.favorite>.Array;
                    }
                    return top5;
                }
            }
        }
        /// <summary>
        /// OJ问题
        /// </summary>
        public diantou.dataAccess.problem Problem;
        /// <summary>
        /// 用户信息
        /// </summary>
        public diantou.dataAccess.user User;
        /// <summary>
        /// 竞赛编号
        /// </summary>
        public char ContestLetter;
        /// <summary>
        /// OJ问题标识
        /// </summary>
        [fastCSharp.code.cSharp.webView.outputAjax]
        public int Id
        {
            get { return Problem == null ? 0 : Problem.Id; }
        }
        /// <summary>
        /// 用户标识
        /// </summary>
        internal int UserId
        {
            get { return User == null ? 0 : User.Id; }
        }
        /// <summary>
        /// 是否AC问题
        /// </summary>
        private bool? isAccepted;
        /// <summary>
        /// 是否AC问题
        /// </summary>
        public bool IsAccepted
        {
            get
            {
                if (Problem == null || User == null) return false;
                if (isAccepted == null)
                {
                    isAccepted = diantou.dataAccess.tcpCall.user.isAcceptedProblem(User.Id, Problem.Id);
                }
                return (bool)isAccepted;
            }
        }
        /// <summary>
        /// 是否存在提交
        /// </summary>
        private bool? isJudge;
        /// <summary>
        /// 是否存在提交
        /// </summary>
        public bool IsJudge
        {
            get
            {
                if (Problem == null || User == null) return false;
                if (isAccepted == null || !(bool)isAccepted)
                {
                    if (isJudge == null) isJudge = diantou.dataAccess.tcpCall.user.isJudgeProblem(User.Id, Problem.Id);
                    return (bool)isJudge;
                }
                return true;
            }
        }
        /// <summary>
        /// 是否提交后没解决问题
        /// </summary>
        public bool NotAccepted
        {
            get
            {
                return !IsAccepted && IsJudge;
            }
        }
        /// <summary>
        /// 用户是否关注问题
        /// </summary>
        public bool IsFocus
        {
            get
            {
                return User != null && Problem != null && diantou.dataAccess.tcpCall.focusProblem.IsFocus(User.Id, Problem.Id, dataModel.focus.type.Focus);
            }
        }
        /// <summary>
        /// 用户是否用户使用点头盾查看题解
        /// </summary>
        public bool IsSolution
        {
            get
            {
                return User != null && Problem != null && diantou.dataAccess.tcpCall.focusProblem.IsFocus(User.Id, Problem.Id, dataModel.focus.type.Solution);
            }
        }
        /// <summary>
        /// 是否可提交代码
        /// </summary>
        public bool CanSubmit
        {
            get
            {
                return User != null && (Problem.ShowType == dataModel.problem.showType.Show || User.IsOnlineJudgeEditor(Problem));
            }
        }
        /// <summary>
        /// 提交记录地址
        /// </summary>
        public webView.hashUrl SubmitPath
        {
            get
            {
                if (User == null || Problem == null) return Problem.Path.Judge;
                return new diantou.dataModel.path.user { Id = User.Id }.OnlineJudgeProblemSubmit(Problem.Id);
            }
        }
        /// <summary>
        /// 是否存在编辑权限
        /// </summary>
        public bool IsPermission
        {
            get
            {
                return User != null && Problem != null && (User.Permission == diantou.dataModel.user.permission.Admin || Problem.UserId == User.Id);
            }
        }
        /// <summary>
        /// 是否验题人
        /// </summary>
        public bool IsTestUser
        {
            get { return Problem.TestUserId == UserId; }
        }
        /// <summary>
        /// 分组其它问题
        /// </summary>
        private userProblem[] versionGroupProblems;
        /// <summary>
        /// 分组其它问题
        /// </summary>
        public userProblem[] VersionGroupProblems
        {
            get
            {
                if (versionGroupProblems == null)
                {
                    diantou.dataAccess.problem[] values = diantou.dataAccess.clientCache.Problem.Get(diantou.dataAccess.tcpCall.problem.getVersionGroupProblemIds(Problem.Id));
                    int index = 0;
                    versionGroupProblems = new userProblem[values.Length];
                    foreach (diantou.dataAccess.problem value in values) versionGroupProblems[index++] = new userProblem { Problem = value, User = User };
                }
                return versionGroupProblems;
            }
        }
        /// <summary>
        /// 提交数量
        /// </summary>
        public int JudgeCount
        {
            get
            {
                if (User == null || Problem == null) return 0;
                return diantou.dataAccess.tcpCall.judge.getUserProblemCount(User.Id, Problem.Id);
            }
        }
        /// <summary>
        /// 测试得分
        /// </summary>
        public int JudgePoint
        {
            get
            {
                if (Problem == null || User == null) return 0;
                return diantou.dataAccess.tcpCall.judge.getUserProblemPoint(User.Id, Problem.Id);
            }
        }
        /// <summary>
        /// 比赛提交记录地址
        /// </summary>
        public webView.hashUrl ContestSubmitPath
        {
            get
            {
                if (User == null || Problem == null || Problem.ContestId == 0) return Problem.Path.ContestJudge;
                return new diantou.dataModel.path.problem { Id = Problem.Id }.ContestSubmitList(User.Id);
            }
        }
        /// <summary>
        /// 竞赛提交数量
        /// </summary>
        public int ContestJudgeCount
        {
            get
            {
                if (User == null || Problem == null) return 0;
                return diantou.dataAccess.tcpCall.contestJudge.getUserProblemCount(User.Id, Problem.Id);
            }
        }
        /// <summary>
        /// 是否AC比赛问题
        /// </summary>
        private bool? contestIsAccepted;
        /// <summary>
        /// 是否AC比赛问题
        /// </summary>
        public bool ContestIsAccepted
        {
            get
            {
                if (Problem == null || User == null) return false;
                if (contestIsAccepted == null)
                {
                    contestIsAccepted = diantou.dataAccess.tcpCall.user.isAcceptedContestProblem(User.Id, Problem.Id);
                }
                return (bool)contestIsAccepted;
            }
        }
        /// <summary>
        /// 是否存在比赛提交
        /// </summary>
        private bool? contestIsJudge;
        /// <summary>
        /// 是否存在比赛提交
        /// </summary>
        public bool ContestIsJudge
        {
            get
            {
                if (Problem == null || User == null) return false;
                if (contestIsAccepted == null || !(bool)contestIsAccepted)
                {
                    if (contestIsJudge == null) contestIsJudge = diantou.dataAccess.tcpCall.user.isJudgeContestProblem(User.Id, Problem.Id);
                    return (bool)contestIsJudge;
                }
                return true;
            }
        }
        /// <summary>
        /// 是否提交后没解决比赛问题
        /// </summary>
        public bool ContestNotAccepted
        {
            get
            {
                return !ContestIsAccepted && ContestIsJudge;
            }
        }
    }
}
